package com.tangkun.environment.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.QiYeGuanLiAdapter;
import com.tangkun.environment.application.Session;
import com.tangkun.environment.entity.base.CommonReturn;
import com.tangkun.environment.entity.personcenter.BindReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.view.CommonDialog;
import com.tangkun.environment.view.CustomListView;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 企业管理 列表
 */

public class QiYeGuanLiActivity extends BaseActivity implements CustomListView.OnRefreshListener{

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.custom_list_view)
    CustomListView customListView;
    private Context mContext;
    /**
     * 用户ID
     */
    private String id = "";

    /**
     * 数据源
     */
    private List<BindReturn.DataBean> dataList = new ArrayList<BindReturn.DataBean>();
    /**
     * 适配器
     */
    private QiYeGuanLiAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_qiyeguanli);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initView();
        initLitener();
        title.setText("企业管理");
    }

    @Override
    protected void onResume() {
        super.onResume();
        bindIndex();
    }

    private void initView() {
        customListView.setOnRefreshListener(this);
        adapter = new QiYeGuanLiAdapter(QiYeGuanLiActivity.this, dataList);
        customListView.setAdapter(adapter);
    }

    private void initLitener() {
        customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent intent = new Intent(QiYeGuanLiActivity.this, QiYeZhongXinActivity.class);
                intent.putExtra("id", dataList.get(position - 1).ids);
                JumpHelper.jump(QiYeGuanLiActivity.this, intent, false);
            }
        });

        customListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
                CommonDialog commonDialog = new CommonDialog(QiYeGuanLiActivity.this, "解除绑定", "确定要解除绑定吗？");
                commonDialog.show();
                commonDialog.setOnClickListener(new CommonDialog.OnClickListener() {
                    @Override
                    public void leftClick() {//取消

                    }

                    @Override
                    public void rightClick() {//确认
                        bindDelete(dataList.get(position - 1).bindids);
                    }
                });
                return true;
            }
        });
    }

    @OnClick({R.id.back, R.id.btn_qiyeguanli_bangdingyiqi})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                JumpHelper.finish(mContext);
                break;
            case R.id.btn_qiyeguanli_bangdingyiqi://绑定企业
                JumpHelper.jump(QiYeGuanLiActivity.this, GuanLianQiYeActivity.class, false);
                break;
        }
    }

    @Override
    public void onRefresh() {
        bindIndex();
    }

    /**
     * 绑定的企业列表
     */
    private void bindIndex() {
        fromNetwork.bindIndex().compose(new RxActivityHelper<BindReturn>().ioMain(QiYeGuanLiActivity.this, true))
                .subscribe(new MySubscribe<BindReturn>(mContext, true) {
                    @Override
                    protected void success(BindReturn resultBean) {
                        if (resultBean.data != null) {
                            customListView.onRefreshComplete();
                            if (resultBean.data.size() == 0) {
                                customListView.setVisibility(View.GONE);
                            } else {
                                customListView.setVisibility(View.VISIBLE);
                            }
                            dataList.clear();
                            //填充数据
                            dataList.addAll(resultBean.data);
                            //刷新列表
                            adapter.notifyDataSetChanged();
                            //如果和用户登录绑定的企业数量不一样 可能是绑定了企业或者解除绑定了企业 这个时候重新调用一次登录接口 并且刷新首页
                            if (dataList.size() != Session.getInstance().getUserBindlist().size()){
                                login(QiYeGuanLiActivity.this);
                            }
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }

    /**
     * 解除绑定
     *
     * @param bindids
     */
    private void bindDelete(final String bindids) {
        fromNetwork.bindDelete(bindids).compose(new RxActivityHelper<CommonReturn>().ioMain(QiYeGuanLiActivity.this, true))
                .subscribe(new MySubscribe<CommonReturn>(mContext, true) {
                    @Override
                    protected void success(CommonReturn resultBean) {
                        showToast("解绑成功");
                        //绑定的企业列表
                        bindIndex();
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //绑定企业成功刷新
    @Subscriber(tag = "QiYeGuanLiActivity.refresh")
    private void refresh(boolean flag) {
        bindIndex();
    }
}
